//
//  SixViewController.m
//  fp
//
//  Created by xingshuai on 2020/11/10.
//

#import "SixViewController.h"

#import "OneTableViewCell.h"         // 反馈类型
#import "TwoTableViewCell.h"         // 问题描述
#import "ThreeTableViewCell.h"       // 反馈图片
#import "FourTableViewCell.h"        // 联系方式

@interface SixViewController () <UITableViewDelegate,UITableViewDataSource,ClickFeedTextHeightDelegate>
/** 列表视图 */
@property (nonatomic, strong) UITableView *tableView;
/** 记录反馈内容高度 */
@property (nonatomic, assign) CGFloat text_h;
/** 记录选中反馈按钮内容 */
@property (nonatomic, strong) NSString *feedBtnTypeStr;
/** 记录反馈内容 */
@property (nonatomic, strong) NSString *feedString;
/** 记录反馈图片 */
@property (nonatomic, strong) NSMutableArray *feedImgList;
/** 记录联系方式 */
@property (nonatomic, strong) NSString *feedContactStr;

@end

@implementation SixViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.feedBtnTypeStr = @"";
    self.feedString = @"";
    self.feedContactStr = @"";
    // 反馈内容默认高度
    self.text_h = [self getWidthWithText:FeedDescrition width:ScreenWidth*0.7-30 font:[UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16]];
    
    [self creatViewUI];
    
}

-(void)creatViewUI {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
}

#pragma mark -- table
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
        
//        UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEditOut)];
//        [_tableView addGestureRecognizer:tapp];
    }
    return _tableView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 1 ? 2 : 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 320;
    } else if (indexPath.section == 1) {
        return indexPath.row == 0 ? self.text_h + 88 + 16 : 125;
    } else {
        return 130;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return section == 2 ? 90 : 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *fotLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
    fotLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    
    UIView *fotView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 90)];
    fotView.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"提 交" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];;
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    [button setBackgroundColor:[UIColor colorWithHexString:@"#86ADFF"]];
    [button addTarget:self action:@selector(clickSubmit) forControlEvents:UIControlEventTouchUpInside];
    [fotView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(fotView);
        make.width.mas_equalTo(self.tableView.frame.size.width-30);
        make.height.mas_equalTo(52);
    }];
    
    return section == 2 ? fotView : fotLine;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MJWeakSelf
    if (indexPath.section == 0) {
        OneTableViewCell *cell = [OneTableViewCell cellWithTableView:tableView];
        cell.didSelectFeedBtnTypeBlock = ^(NSString * _Nonnull feedBtnText) {
            weakSelf.feedBtnTypeStr = feedBtnText;
        };
        return cell;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            TwoTableViewCell *cell = [TwoTableViewCell cellWithTableView:tableView];
            cell.delegate = self;
            cell.feedString = self.feedString;
            return cell;
        } else {
            ThreeTableViewCell *cell = [ThreeTableViewCell cellWithTableView:tableView];
            cell.didSelectFeedImageListBlock = ^(NSMutableArray * _Nonnull imgList) {
                weakSelf.feedImgList = imgList;
            };
            return cell;
        }
    } else {
        FourTableViewCell *cell = [FourTableViewCell cellWithTableView:tableView];
        cell.didSelectContactInformationBlock = ^(NSString * _Nonnull contactText) {
            weakSelf.feedContactStr = contactText;
        };
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [self.view endEditing:YES];
}

#pragma mark -- ClickFeedTextHeightDelegate
-(void)editFeedContentTextString:(NSString *)textString
{
    self.text_h = [self getWidthWithText:textString width:ScreenWidth*0.7-30 font:[UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16]];
        
    // 去空格
    self.feedString = [textString stringByReplacingOccurrencesOfString:@" " withString:@""];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

}

// 获取文字高度
-(CGFloat)getWidthWithText:(NSString*)text width:(CGFloat)wid font:(UIFont *)font{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(wid, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    return rect.size.height+2;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //[self clickEditOut];
}

// 退出编辑
-(void)clickEditOut {
    [self.view endEditing:YES];
}

// 提交反馈响应
-(void)clickSubmit {
    if ([NSString trimEmpty:self.feedBtnTypeStr]) {
        // 请选择反馈类型
    } else if ([NSString trimEmpty:self.feedString]) {
        // 请填写问题/意见描述
    } else if ([NSString trimEmpty:self.feedContactStr]) {
        // 请填写联系方式
    } else {
        NSLog(@"feedBtnTypeStr = %@,feedString = %@,feedContactStr = %@",self.feedBtnTypeStr,self.feedString,self.feedContactStr);
        NSString *strImg = @"";
        if (self.feedImgList.count > 0) {
            strImg = [self.feedImgList componentsJoinedByString:@","];
        }
        NSLog(@"img = %@",strImg);
        /**
        // 网络可用
        if ([XAClient sharedClient].isNetWorkAvailable) {
            // 汉字序列化后上上传，否则上传不成功
            NSString *feedBtnTypeStr = [self.feedBtnTypeStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            NSString *feedString = [self.feedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            NSString *feedContactStr = [self.feedContactStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            // 接口参数
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:[XAClient sharedClient].currentRole.roleId forKey:@"roleId"];
            [dict setValue:feedContactStr forKey:@"contact"];
            [dict setValue:feedBtnTypeStr forKey:@"type"];
            [dict setValue:feedString forKey:@"description"];
            [dict setValue:strImg forKey:@"urlAddress"];
            // 开始通过接口上传
            [[XAClient sharedClient] backgroundPOST:@"/cms/api/u/feedBack/upload" withParam:dict success:^(id responseObject) {
                if ([[responseObject objectForKey:@"ss"] boolValue]) {
                    [XAAlertView toast:@"提交成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                } else {
                    [XAAlertView toast:@"提交失败,请稍后重新上传"];
                }
            } failure:^(NSError *error) {
                [XAAlertView toast:@"提交失败,请稍后重新上传"];
            }];
        } else {
            [XAAlertView toast:@"网络错误"];
        }
         */
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
